﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SocialFront.Entities;
using SocialFront.Providers.Member;
using SocialFront.Providers.Relationship;
using umbApi = umbraco;

namespace SocialFront.Web.usercontrols.Membership
{
    public partial class EditMemberProfile : System.Web.UI.UserControl
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            ((umbApi.UmbracoDefault)Page).ValidateRequest = false;
            Utilities.AddTinyMce();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                Member member = MemberProvider.Provider.GetCurrentMember();
                DOBDay.Items.Add(new ListItem("Day", "."));
                for (int i = 1; i <= 31; i++)
                {
                    DOBDay.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }
                DOBmonth.Items.Add(new ListItem("Month", "."));
                for (int i = 1; i <= 12; i++)
                {
                    DOBmonth.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }
                DOBYear.Items.Add(new ListItem("Year", "."));
                for (int i = (DateTime.Now.Year - 16); i >= 1900; i--)
                {
                    DOBYear.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }

                Email.Text = member.Email;
                tbAboutMe.Text = member.AboutMe;

                GetDropdownItem(member.DateOfBirth.Day, DOBDay);
                GetDropdownItem(member.DateOfBirth.Month, DOBmonth);
                GetDropdownItem(member.DateOfBirth.Year, DOBYear);
            }
        }

        private void GetDropdownItem(int getValue, DropDownList dd)
        {
            foreach (ListItem item in dd.Items)
            {
                int date;
                if (int.TryParse(item.Value, out date))
                {
                    if (date == getValue)
                        item.Selected = true;
                }
            }
        }

        protected void UpdateProfile_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                DateTime userDob;
                string dobDate = DOBDay.SelectedValue + "/" + DOBmonth.SelectedValue + "/" +
                                 DOBYear.SelectedValue;
                if (DateTime.TryParse(dobDate, out userDob))
                {
                    Member member = MemberProvider.Provider.GetCurrentMember();
                    member.Email = Email.Text;
                    member.AboutMe = tbAboutMe.Text;
                    member.DateOfBirth = userDob;

                    MemberProvider.Provider.UpdateMember(member);

                    Response.Redirect(member.MemberUrl);
                }
            }
        }
    }
}